/*
 * @Author: your name
 * @Date: 2020-12-08 13:32:28
 * @LastEditTime: 2021-05-20 16:45:55
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /medicine-web/src/store/modules/app.js
 */
import Vue from 'vue'
const app = {
  state: {},
  actions: {
    getFileBloc({ commit }, data) {
      var xhr = new XMLHttpRequest()
      xhr.open('GET', process.env.VUE_APP_BASE_API + `user/get_download_file?ids=${data.id}&user_id=${data.user_id}&module=${data.module}`, true)
      xhr.responseType = 'arraybuffer'
      xhr.onload = function(e) {
        if (this.status == 200) {
          const vue = new Vue()
          vue.msgSuccess('下载成功！')
          var blob = new Blob([this.response])
          var link = document.createElement('a')
          link.href = window.URL.createObjectURL(blob)
          let headerfileName = xhr.getResponseHeader('Content-Disposition')
          console.log('headerfileName', headerfileName)
          if (headerfileName) {
            link.download = headerfileName.split('=')[1].replace(/\"/g, "")
          } else {
            link.download = '未知文件.txt'
          }
          link.click()
        }
      }
      xhr.send()
    },
    getFileBlocBatch({ commit }, data) {
      var xhr = new XMLHttpRequest()
      xhr.open('GET', process.env.VUE_APP_BASE_API + `user/get_download_file?ids=${data.ids}&user_id=${data.user_id}&module=${data.module}`, true)
      xhr.responseType = 'arraybuffer'
      xhr.onload = function(e) {
        if (this.status == 200) {
          const vue = new Vue()
          vue.msgSuccess('批量下载成功!')
          var blob = new Blob([this.response])
          var link = document.createElement('a')
          link.href = window.URL.createObjectURL(blob)
          let headerfileName = xhr.getResponseHeader('Content-Disposition')
          console.log('headerfileName', headerfileName)
          if (headerfileName) {
            link.download = headerfileName.split('=')[1].replace(/\"/g, "")
          } else {
            link.download = '未知文件.zip'
          }
          link.click()
        }
      }
      xhr.send()
    }
  },
  mutations: {},
  getters: {}
}

export default app
